package edu.umd.ion.wavemeter.server.jep;

import java.util.Stack;

import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.PostfixMathCommand;

public class Rectangle extends PostfixMathCommand {
	public Rectangle() {
		numberOfParameters = 1;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void run(Stack inStack) throws ParseException {
		   // check the stack
		   checkStack(inStack);
		   
		   // get the parameter from the stack
		   Object param = inStack.pop();

		   // check whether the argument is of the right type
		   if (param instanceof Double) {
		      // calculate the result
		      double r = ((Double)param).doubleValue();
		      r -= Math.floor(r);
		      r = r<0.5 ? 0 : 1;
		      
		      // push the result on the inStack
		      inStack.push(new Double(r)); 
		   } else {
		      throw new ParseException("Invalid parameter type");
		   }

	}

}
